package edu.scau.misp.backend_module.controller;


import edu.scau.misp.backend_module.core.domain.AjaxResult;
import edu.scau.misp.backend_module.domain.Category;
import edu.scau.misp.backend_module.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"category"})
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    @GetMapping("/{categoryId}")
    public AjaxResult getCategoryById(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(categoryService.getCategoryById(categoryId));
    }
    @GetMapping("/listAll")
    public AjaxResult listAllCategory(){
        return AjaxResult.success(categoryService.listAllCategory());
    }

    @GetMapping("/list")
    public AjaxResult list(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        return AjaxResult.success(list);
    }

    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        int rows = categoryService.insertCategory(category);
        return rows > 0 ? AjaxResult.success("添加分类成功") : AjaxResult.error("添加分类失败");
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        int rows = categoryService.updateCategory(category);
        return rows > 0 ? AjaxResult.success("修改分类成功") : AjaxResult.error("修改分类失败");
    }

    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable("categoryId") Long categoryId) {
        int rows = categoryService.deleteCategoryById(categoryId);
        return rows > 0 ? AjaxResult.success("删除分类成功") : AjaxResult.error("删除分类失败");
    }

}
